<template>
	<view class="">
		<Header title="购买会员" :isHome="true" :isWhite='true'></Header>
		<view class="userInfo">
			<view class="">
				<image src="/static/avator.png" v-if="!userInfo.avatar" mode="" class="avator"></image>
				<!-- <image src="/static/avator.png" mode="" class="avator"></image> -->
				<image v-else @click="handleLook(img_url+userInfo.avatar)" :src="img_url+userInfo.avatar" mode="" class="avator"></image>
			</view>
			<view class="">
				<view class="name">
					{{userInfo.adultname?userInfo.adultname:userInfo.mobile}}
				</view>
				<view class="huiyuanstatus" v-if="userInfo.level==0">
					暂时还未开通会员
				</view>
				<view class="huiyuanstatus" v-else>
					<!-- 会员等级{{userInfo.level}} -->
					{{userInfo.level_name}}
				</view>
			</view>
		</view>
		<view class="buyhuiyuan">
			<view class="title">
				购买会员
			</view>
			<view class="annual">
				<view class="annual-con">
					{{info.title}}
				</view>
				<view class="annual-money">
					{{info.price}}元
				</view>
			</view>
			<view class="member">
				<view class="member-title">
					会员权益
				</view>
				<view class="member-con">
					{{info.content}}
				</view>
				<!-- <view class="member-con">
					1、注意事项注意事项注意事项注意事项注意事项注意事项;
				</view> -->
			</view>
			<view class="payment" @click="toPay()">
				立即支付
			</view>
		</view>

	</view>
</template>

<script>
	import {
		get,
		post,
		img_url
	} from "../../utils/request";
	export default {
		data() {
			return {
				img_url: img_url,
				order_sn: '',
				info: {},
				userInfo: {}
			}
		},
		onLoad() {
			this.getUserInfo()
			this.getlevelinfo()
		},
		methods: {
			// 购买会员 创建订单
			toPay() {
				var that = this
				post('/order/create_order', {
						level_id: this.info.id
					}).then(res => {
						if (res.code == 1) {
							that.order_sn = res.data.order_sn
							// 仅作为示例，非真实参数信息。
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.data.timeStamp,
								nonceStr: res.data.data.nonceStr,
								package: res.data.data.package,
								signType: res.data.data.signType,
								paySign: res.data.data.paySign,
								success: function(res) {
									uni.showToast({
										title: '支付成功'
									})
									console.log('success:' + JSON.stringify(res));
								},
								fail: function(err) {
									uni.showToast({
										title: '支付失败',
										icon: 'none'
									})
									console.log('fail:' + JSON.stringify(err));
								},
								complete: function(res) {
									setTimeout(function() {
										uni.switchTab({
											url: '/pages/my/index'
										})
									}, 2000)
									setTimeout(function() {
										that.checkOrder()
									}, 3000)
								}
							});
						} else {
							uni.showToast({
								title: '请求错误',
								icon: 'none'
							})
						}
					})
					.catch(err => {
						uni.showToast({
							title: '请求错误',
							icon: 'none'
						})
					});
			},
			// 获取订单支付状态
			checkOrder() {
				get('/order/check_order?order_sn=' + this.order_sn).then(res => {
					console.log(res);
				}).catch(err => {
					console.log(err);
				});
			},
			getUserInfo() {
				get('/user/user_info')
					.then(res => {
						this.userInfo = res.data
					})
					.catch(err => {
						console.error('GET 请求失败:', err);
					});
			},
			getlevelinfo() {
				get('/user/levelinfo')
					.then(res => {
						this.info = res.data
					})
					.catch(err => {
						console.error('GET 请求失败:', err);
					});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.userInfo {
		background: url(/static/join.png) no-repeat center center / cover;
		padding: 64rpx 40rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;

		.avator {
			width: 88rpx;
			height: 88rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.name {
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 28rpx;
			color: #FFFFFF;
		}

		.huiyuanstatus {
			font-family: PingFangSC-Regular;
			font-weight: 400;
			font-size: 24rpx;
			color: #FFFFFF;
			margin-top: 10rpx;
		}

	}

	.buyhuiyuan {
		background-color: #FFFFFF;
		border-radius: 24rpx 24rpx 0 0;
		position: relative;
		top: -15rpx;
		padding: 40rpx 32rpx;
		height: 1160rpx;


		.title {
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 36rpx;
			color: #222222;
		}

		.annual {
			width: 686rpx;
			height: 138rpx;
			background: #FFF7E1;
			border: 3rpx solid #FED35C;
			border-radius: 12rpx 12rpx 25rpx 12rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #222222;
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			margin: 32rpx 0;

			.annual-con {
				font-size: 28rpx;
				margin-right: 20rpx;
			}

			.annual-money {
				font-size: 52rpx;
			}
		}

		.member {
			width: 686rpx;
			height: 254rpx;
			background: #F8F8F8;
			border: 1rpx solid #DDDDDD;
			border-radius: 24rpx;
			color: #999;
			padding: 28rpx;
			box-sizing: border-box;

			.member-title {
				font-family: PingFangSC-SNaNrpxibold;
				font-weight: 600;
				font-size: 30rpx;
				margin-bottom: 12rpx;
			}

			.member-con {
				font-family: PingFangSC-Regular;
				font-weight: 400;
				font-size: 26rpx;
				line-height: 36rpx;
			}
		}

		.payment {
			width: 686rpx;
			height: 96rpx;
			background: #FFD275;
			border-radius: 48rpx;
			margin: 32rpx auto;
			text-align: center;
			line-height: 96rpx;
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 32rpx;
			color: #1A1A1A;
		}
	}
</style>